# coding: utf-8
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
# 1、获得数据集
def getDataFromNetwork():
    iris = load_iris()

    # 2、描述数据集
    print("特征值：\n",iris.data)
    print("目标值：\n",iris['target'])
    print("特征值名称：\n",iris.feature_names)
    print("目标值名称：\n",iris.target_names)
    print("数据集描述L\n",iris.DESCR)


    return iris

def iris_plot(data,col1,col2):
    sns.lmplot(x=col1,y=col2,data=data,hue='target',fit_reg=False)
    plt.title('iris data Show')
    plt.show()


if __name__ == '__main__':
    iris = getDataFromNetwork()
    # 数据可视化
    iris_d = pd.DataFrame(data=iris.data, columns=['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width'])
    iris_d['target'] = iris.target
    iris_plot(iris_d,'Sepal_Width','Petal_Length')
    iris_plot(iris_d,'Sepal_Length','Petal_Width')

    #划分数据集
    x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,test_size=0.2,random_state=22)
    print("训练集的特征值是:\n", x_train)
    print("训练集的目标值是:\n", y_train)
    print("测试集的特征值是:\n", x_test)
    print("测试集的目标值是:\n", y_test)

    # 特征工程
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)

    #  机器学习-knn
    # 定义一个K值范围


    estimator = KNeighborsClassifier(n_neighbors=5)
    estimator.fit(x_train,y_train)

    # 模型评估
    # 5.1 预测值结果输出
    y_pre = estimator.predict(x_test)
    print("预测值：\n",y_pre)
    print("预测值和真实值的对比是:\n",y_pre==y_test)

    # 5.2 准确率计算
    score = estimator.score(x_test,y_test)
    print("准确率:\n",score)

